﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Net.Http;


namespace HydrographicSurvey.HYTCPServer {
    public class HttpClientService : IClientService {
        private static readonly HttpClient _client = new HttpClient();
        public HttpClient GetHttpClient {
            get {
                return _client;
            }
        }

        public LogDTO CoventToLog(byte[] bytes) {

            LogDTO logDto = new LogDTO();
            logDto.DeviceId = string.Format("{0:X2}-{1:X2}-{2:X2}-{3:X2}-{4:X2}-{5:X2}", bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5]);

            string d1 = "20" + bytes[6] + "-" + bytes[7] + "-" + bytes[8] + " " + bytes[9] + ":" + bytes[10] + ":" + bytes[11];
            string d2 = "20" + bytes[12] + "-" + bytes[13] + "-" + bytes[14] + " " + bytes[15] + ":" + bytes[16] + ":" + bytes[17];
            DateTime createDate1;
            DateTime createDate2;

            if (!DateTime.TryParse(d1, out createDate1)) {
                createDate1 = DateTime.Now;
            }
            if (!DateTime.TryParse(d2, out createDate1)) {
                createDate2 = DateTime.Now;
            }

            int voltage = bytes[19];
            logDto.Voltage = (double)voltage / 10;

            int signalStrength = bytes[21];
            logDto.SignalStrength = signalStrength;

            int count = bytes[23];

            int type = bytes[24];
            int rainfall = BitConverter.ToInt16(new byte[] { bytes[27], bytes[26] }, 0);
            logDto.Rainfall = (double)rainfall / 100;

            int type2 = bytes[28];
            int water = BitConverter.ToInt16(new byte[] { bytes[31], bytes[30] }, 0);
            logDto.WaterLevel = (double)water / 100;
            return logDto;
        }

        private List<KeyValuePair<string, string>> GetEntityToListKeyValuePair(LogDTO log) {
            var keyvalus = new List<KeyValuePair<string, string>>();

            foreach (PropertyInfo po in
                log.GetType().GetProperties()) {
                object rr = po.GetValue(log, null);

                if (rr != null && !string.IsNullOrWhiteSpace(rr.ToString())) {
                    // parse datatime and guid and bool type has some issue
                    if (!rr.ToString().Contains('/') && /*!rr.ToString().Contains('-') &&*/ !rr.ToString().Contains("False")) {
                        keyvalus.Add(new KeyValuePair<string, string>(po.Name, rr.ToString()));
                        //Console.WriteLine("{0}:  {1}", po.Name, rr);
                    }
                }
            }

            return keyvalus;
        }

        public void LogAddService(string uri, LogDTO logDto) {

            //using (HttpClient client = new HttpClient()) {
            var content = new FormUrlEncodedContent(GetEntityToListKeyValuePair(logDto));
            GetHttpClient.PostAsync(uri, content);//.ContinueWith((a) => client.Dispose());

            //}
            //client.Dispose();
            //Console.WriteLine("Hit ENTER to exit...");
            //Console.ReadLine();
        }
    }
}
